package cn.sxt.usercenter.head.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;

import cn.sxt.usercenter.api.pojo.AAdmins;
import cn.sxt.usercenter.api.pojo.AUsers;
import cn.sxt.usercenter.api.pojo.AUsersEnum;
import cn.sxt.usercenter.api.service.IUserService;
import cn.sxt.usercenter.common.controller.BaseController;
import cn.sxt.usercenter.common.util.ConstantFinally;
import cn.sxt.usercenter.common.util.PageInfoUtil;

//不需要的登录
@Controller
public class NoLoginController extends BaseController
{
	
	@Resource
	private IUserService userService;
	
	//管理员列表面
	@RequestMapping("/adminsList")
	public String adminsList(HttpServletRequest request)
	{
		ConstantFinally.LOGGER.info("----adminsList---");
		PageInfoUtil pageInfoUtil=new PageInfoUtil();
		//按照id查询
		Map<String,Object> condMap=new HashMap<String,Object>();
		List<AAdmins> adminsList=this.userService.findCondListAdminsService(pageInfoUtil, condMap);
		//存储结果
		request.setAttribute("adminsList", adminsList);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		System.out.println("NoLoginController.adminsList(---------------------)");
		return "/head/adminsList";
		
	}
	
	@RequestMapping("/login")
	public String login()
	{
		ConstantFinally.LOGGER.info("------login------");
		return "/head/login";
		
	}
	
	//打开登录页面
	@RequestMapping("/loginSubmit")
	public String loginSubmit(HttpServletRequest request ,HttpSession session,String id)
	{
		ConstantFinally.LOGGER.info("------loginSubmit------");
		String email=request.getParameter("email");
		System.out.println("NoLoginController.loginSubmit(11111111111)"+email);
		String password=request.getParameter("password");
		String code=request.getParameter("code");
		
		//从session中取验证码
		String sessCode=session.getAttribute("rand")+"";
		
		//验证码比对完成，从session中移除
		session.removeAttribute("rand");
		
		if(sessCode !=null && sessCode.equalsIgnoreCase(code))
		{
			//根据用户名查询用户
			Map<String,Object> condMap=new HashMap<String,Object>();
			condMap.put("email", email);
			AUsers users=this.userService.findOneUsersService(condMap);
			if(users != null)
			{
				System.out.println("NoLoginController.loginSubmit(111111)"+users.getPassword());
				//邮箱存在
				if(this.encryptUtil.decode(password, users.getPassword()))
				{
					System.out.println("NoLoginController.loginSubmit(22222)");
					if(users.getStatus() == AUsersEnum.STATUS_ENABLED.getStatus())
					{
						System.out.println("NoLoginController.loginSubmit(33333333333)");
						//登录成攻    放入session
						session.setAttribute("users", users);
						session.setAttribute("lastLoginTime", users.getLastLoginTime());
						
						users.setLastLoginTime(new Date());
						this.userService.updateOneUsers(users);
						
						//客户端跳转到登陆成功页面
						return "redirect:/head/users/main.htm";
					}else
					{
						//账户被禁用
						this.info="账户被禁用";
					}
				}else
				{
					//密码不正确
					this.info="密码不正确";
					
				}
				request.setAttribute("email", email);
				
			}else
			{
				//邮箱不存在
				this.info="邮箱不存在";
			}
			
		}else
		{
			//验证码不正确
			this.info="验证码不正确";
		}
		request.setAttribute("info", info);
	
		/*if("1".equalsIgnoreCase(id))
		{
			return "redirect:/head/users/main.htm";
		}*/
		return this.login();
	}
	
	//打开注册页面
	@RequestMapping("/register")
	public String register()
	{
		ConstantFinally.LOGGER.info("--------register-------");
		return "/head/register";
	}
	/**
	 * 注冊提交頁面
	 * */
	@RequestMapping("/registerSubmit")
	public String registerSubmit(HttpServletRequest request,HttpSession session)
	{
		ConstantFinally.LOGGER.info("-------registerSubmit-------");
		String email =request.getParameter("email");
		String password=request.getParameter("password");
		String code=request.getParameter("code");
		
		//从session 中获取验证码
		String sessCode=session.getAttribute("rand")+"";
		
		/* 验证码比对完成,从session中移除 */
		session.removeAttribute("rand");
		
		if(sessCode != null && sessCode.equalsIgnoreCase(sessCode))
		{
			AUsers user=new AUsers();
			user.setEmail(email);
			user.setPassword(this.encryptUtil.encode("password"));
			//刚注册时都是启用
			user.setStatus(AUsersEnum.STATUS_ENABLED.getStatus());
			user.setCreateTime(new Date());
			user.setUpdateTime(new Date());
			user.setLastLoginTime(new Date());
			
			JSONObject resultJSON=this.userService.saveOneUsers(user);
			this.info=resultJSON.getString("info");
			
		}else
		{
			this.info="验证码不正确";
		}
		request.setAttribute("info", info);
		return this.register();
	}
	
	
}
